﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.Utilities;
using System.IO;

namespace SUIE.ControlTemplates
{
    public partial class AccordionConfigurationEditorPartUserControl : System.Web.UI.UserControl
    {
        private AccordionConfigurationEditorPart parentEditorPart;

        public Boolean AccordionEnabled
        {
            get
            {
                return chkEnabled.Checked;
            }
            set
            {
                chkEnabled.Checked = value;
            }
        }

        public Boolean AccordionFirstItemOpen
        {
            get
            {
                return chkFirstItemOpen.Checked;
            }
            set
            {
                chkFirstItemOpen.Checked = value;
            }
        }

        public Boolean AccordionAutoHeight
        {
            get
            {
                return chkAutoHeight.Checked;
            }
            set
            {
                chkAutoHeight.Checked = value;
            }
        }
        public Boolean AccordionClearStyle
        {
            get
            {
                return chkClearStyle.Checked;
            }
            set
            {
                chkClearStyle.Checked = value;
            }
        }

        public Boolean AccordionCollapsible
        {
            get
            {
                return chkCollapsible.Checked;
            }
            set
            {
                chkCollapsible.Checked = value;
            }
        }

        public String AccordionIcon
        {
            get
            {
                return ddlIcon.SelectedValue;
            }
            set
            {
                ddlIcon.SelectedValue = value;
            }
        }

        public String AccordionEvent
        {
            get
            {
                return ddlEvent.SelectedValue;
            }
            set
            {
                ddlEvent.SelectedValue = value;
            }
        }

        public String AccordionTheme
        {
            get
            {
                return ddlTheme.SelectedValue;
            }
            set
            {
                if (ddlTheme.Items.Count == 0)
                    GetThemes();
                ddlTheme.SelectedValue = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.parentEditorPart = this.Parent as AccordionConfigurationEditorPart;

            // Check whether this is the first Page_Load of the control.
            if (this.hiddenFieldDetectRequest.Value == "0")
            {
                this.hiddenFieldDetectRequest.Value = "1";
            }

        }

        private void ApplyChanges()
        {
            // Call the ApplyChanges method of the Parent Editor Part class.    
            this.AccordionEnabled = chkEnabled.Checked;
            this.AccordionFirstItemOpen = chkFirstItemOpen.Checked;
            this.AccordionAutoHeight = chkAutoHeight.Checked;
            this.AccordionClearStyle = chkClearStyle.Checked;
            this.AccordionCollapsible = chkCollapsible.Checked;
            this.AccordionIcon = ddlIcon.SelectedValue;
            this.AccordionEvent = ddlEvent.SelectedValue;
            this.AccordionTheme = ddlTheme.SelectedValue;
            this.parentEditorPart.ApplyChanges();
        }

        private void GetThemes()
        {
            string spSetupPath = SPUtility.GetGenericSetupPath(string.Empty);
            string themesFolderPath = @"TEMPLATE\LAYOUTS\SUIE\css\jquery-ui";
            foreach (string f in Directory.GetDirectories(spSetupPath + themesFolderPath))
            {
                ddlTheme.Items.Add(f.Replace(spSetupPath + themesFolderPath + @"\", ""));
            }

        }

    }
}
